package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.StatusConstant;
import com.sky.dto.RiderDTO;
import com.sky.dto.RiderPageQueryDTO;
import com.sky.dto.RiderVerifyDTO;
import com.sky.entity.Employee;
import com.sky.entity.Orders;
import com.sky.entity.Rider;
import com.sky.mapper.RiderMapper;
import com.sky.result.PageResult;
import com.sky.service.RiderService;
import com.sky.vo.RiderFeeDetailVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

@Service
@Slf4j
public class RiderServiceImpl implements RiderService {

    @Autowired
    private RiderMapper riderMapper;

    @Override
    public void add(RiderDTO riderDTO) {
        Rider rider=new Rider();
        BeanUtils.copyProperties(riderDTO,rider);
        rider.setStatus(StatusConstant.ENABLE);
        riderMapper.add(rider);
    }

    @Override
    public PageResult pagelist(RiderPageQueryDTO riderPageQueryDTO) {
        if(riderPageQueryDTO.getPhone()!=""&&riderPageQueryDTO.getPage()>1){
            riderPageQueryDTO.setPage(1);
//            PageHelper.startPage(employeePageQueryDTO.getPage(),employeePageQueryDTO.getPageSize());

            Page<Rider> riders= riderMapper.pageall(riderPageQueryDTO);
            return new  PageResult(riders.getTotal(),riders.getResult());
        }
        PageHelper.startPage(riderPageQueryDTO.getPage(),riderPageQueryDTO.getPageSize());
        Page<Rider> riders= riderMapper.pageall(riderPageQueryDTO);

//        PageInfo<Employee> employeePageInfo = new PageInfo<>(employees);
        Long total=riders.getTotal();
        List<Rider> records=riders.getResult();

        return new PageResult(total,records);
    }

    @Override
    public void startorstop(Integer status, Long id) {
        Rider rider=Rider.builder()
                .status(status)
                .id(id).build();
        riderMapper.update(rider);
    }

    @Override
    public Rider getbyid(Integer id) {
        return riderMapper.getbyid(id);
    }

    @Override
    public void update(RiderDTO riderDTO) {
        Rider rider=new Rider();
        BeanUtils.copyProperties(riderDTO,rider);
        //employee.setUpdateTime(LocalDateTime.now());
        //employee.setUpdateUser(BaseContext.getCurrentId());
        riderMapper.update(rider);
    }

    @Override
    public Rider verifybyphone(RiderVerifyDTO riderVerifyDTO) {
        Rider rider=riderMapper.getbyphone(riderVerifyDTO.getPhone());
        if(rider!=null&&rider.getOpenid()==null){
            rider.setOpenid(riderVerifyDTO.getOpenid());
            riderMapper.update(rider);
        }
        return rider;
    }

    @Override
    public List<RiderFeeDetailVO> getbalancedetail(String courierid) {
        List<Orders> riders=riderMapper.getallorders(courierid);
        List<RiderFeeDetailVO> feeDetails = new ArrayList<>();
        int totalBalance = 0;
        for (Orders rider : riders) {
            RiderFeeDetailVO feeDetail = new RiderFeeDetailVO();
            // 设置订单时间
            feeDetail.setDeliveryTime(rider.getDeliveryTime());
            // 设置每单配送费
            feeDetail.setDeliveryfee(rider.getDeliveryfee());
            feeDetail.setId(rider.getId());
            // 累加余额
            totalBalance += rider.getDeliveryfee();
            feeDetail.setBalance(totalBalance);
            // 将 RiderFeeDetailVO 对象加入列表
            feeDetails.add(feeDetail);
        }
        // 提取 balance 到列表
        List<Integer> balances = feeDetails.stream()
                .map(RiderFeeDetailVO::getBalance)
                .collect(Collectors.toList());

        // 逆序处理 balances 列表
        List<Integer> reversedBalances = new ArrayList<>(balances);
        Collections.reverse(reversedBalances);

        // 使用 reversedBalances 更新 feeDetails
        IntStream.range(0, feeDetails.size()).forEach(i ->
                feeDetails.get(i).setBalance(reversedBalances.get(i))
        );


        // 返回包含所有 RiderFeeDetailVO 的列表
        return feeDetails;
    }
}
